﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace ScrumMagic
{

    /// <summary>
    /// Interaction logic for LoginWindow.xaml
    /// </summary>
    public partial class LoginWindow : Window
    {
        public static string GetMD5Hash(string input)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
            bs = x.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            string password = s.ToString();
            return password;
        }

        public LoginWindow()
        {
            InitializeComponent();
        }
        
        //public MainWindow MWnd (string myId);

        private void OnCancelButtonClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void OnLoginButtonClick(object sender, RoutedEventArgs e)
        {
            int userId = 0;
            string userLogin = textBox1.Text;
            bool IsUserFound = false;
            bool autoLogin = false;
#if DEBUG
            if (string.IsNullOrEmpty(userLogin))
            {
                userLogin = "admin";
                autoLogin = true;
            }
#endif
            SqlCommand myCommand = DBController.MainConnection.CreateCommand();
            myCommand.CommandText = "SELECT Id,Password FROM [User] WHERE Login LIKE @login;";
            myCommand.CommandType = CommandType.Text;
            myCommand.Prepare();
            myCommand.Parameters.AddWithValue("@login", userLogin);
            using (SqlDataReader UserReader = myCommand.ExecuteReader())
            {
                if(UserReader.Read())
                {
                    if (autoLogin)
                    {
                        IsUserFound = true;
                        userId = (int)UserReader["Id"];
                    }
                    else
                    {
                        string MD5pass = GetMD5Hash(passwordBox1.Password);
                        if (UserReader["Password"].ToString() == MD5pass)
                        {
                            IsUserFound = true;
                            userId = (int)UserReader["Id"];
                        }
                    }
                }
            }

            if (!IsUserFound)
            {
                MessageBox.Show(
                    "Your password is incorrect or you are not registered. Please try again.",
                    "ScrumMagic",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }
            else
            {

                CurrentUser.Set(userId, userLogin);
                MainWindow MWnd = new MainWindow();
                MWnd.Show();
                this.Close();
            }
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
    }
}
